#include "com_nwpusp_jni_JniProxy.h"
#include <stdlib.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct _point {
	jint x;
	jint y;
	jint z;
} POINT;

typedef struct _size {
	jint cx;
	jint cy;
	jint cz;
} SIZE;

jboolean collideJudge(POINT thisPoint, SIZE thisSize, POINT targetPoint, SIZE targetSize) {
	POINT vertex[8];
	POINT thisPolar;
	int i = 0;
	vertex[0].x = targetPoint.x;   vertex[0].y = targetPoint.y;   vertex[0].z = targetPoint.z;
	vertex[1].x = targetPoint.x;   vertex[1].y = targetPoint.y;   vertex[1].z = targetPoint.z + targetSize.cz;
	vertex[2].x = targetPoint.x;   vertex[2].y = targetPoint.y + targetSize.cy;   vertex[2].z = targetPoint.z;
	vertex[3].x = targetPoint.x;   vertex[3].y = targetPoint.y + targetSize.cy;   vertex[3].z = targetPoint.z + targetSize.cz;
	vertex[4].x = targetPoint.x + targetSize.cx;   vertex[4].y = targetPoint.y;   vertex[4].z = targetPoint.z;
	vertex[5].x = targetPoint.x + targetSize.cx;   vertex[5].y = targetPoint.y;   vertex[5].z = targetPoint.z + targetSize.cz;
	vertex[6].x = targetPoint.x + targetSize.cx;   vertex[6].y = targetPoint.y + targetSize.cy;   vertex[6].z = targetPoint.z;
	vertex[7].x = targetPoint.x + targetSize.cx;   vertex[7].y = targetPoint.y + targetSize.cy;   vertex[7].z = targetPoint.z + targetSize.cz;

	thisPolar.x = thisPoint.x + thisSize.cx;
	thisPolar.y = thisPoint.y + thisSize.cy;
	thisPolar.z = thisPoint.z + thisSize.cz;

	for(i = 0; i < 8; i++) {
		if(thisPoint.x <= vertex[i].x && thisPoint.y <= vertex[i].y && thisPoint.z <= vertex[i].z &&
		   thisPolar.x >= vertex[i].x && thisPolar.y >= vertex[i].y && thisPolar.z >= vertex[i].z) {
			return JNI_TRUE;
		}
	}
	return JNI_FALSE;
}

JNIEXPORT jboolean JNICALL Java_com_nwpusp_jni_JniProxy_isCollid
  (JNIEnv* env, jclass cls, jintArray jpa, jintArray jpb, jintArray jsa, jintArray jsb) {

	jint pa[3];
	jint pb[3];
	jint sa[3];
	jint sb[3];
	POINT thisPt;
	SIZE thisSz;
	POINT targetPt;
	SIZE targetSz;

	(*env)->GetIntArrayRegion(env, jpa, 0, 3, pa);
	(*env)->GetIntArrayRegion(env, jpb, 0, 3, pb);
	(*env)->GetIntArrayRegion(env, jsa, 0, 3, sa);
	(*env)->GetIntArrayRegion(env, jsb, 0, 3, sb);

	if(sa[0] > sb[0] && sa[1] > sb[1] && sa[2] > sb[2]) {
		thisPt.x = pa[0];
		thisPt.y = pa[1];
		thisPt.z = pa[2];
		thisSz.cx = sa[0];
		thisSz.cy = sa[1];
		thisSz.cz = sa[2];

		targetPt.x = pb[0];
		targetPt.y = pb[1];
		targetPt.z = pb[2];
		targetSz.cx = sb[0];
		targetSz.cy = sb[1];
		targetSz.cz = sb[2];
	}
	else {
		targetPt.x = pa[0];
		targetPt.y = pa[1];
		targetPt.z = pa[2];
		targetSz.cx = sa[0];
		targetSz.cy = sa[1];
		targetSz.cz = sa[2];

		thisPt.x = pb[0];
		thisPt.y = pb[1];
		thisPt.z = pb[2];
		thisSz.cx = sb[0];
		thisSz.cy = sb[1];
		thisSz.cz = sb[2];
	}
	//return JNI_TRUE;
	return ((collideJudge(thisPt, thisSz, targetPt, targetSz) ==  JNI_TRUE ) || (collideJudge(targetPt, targetSz, thisPt, thisSz) == JNI_TRUE)) ? JNI_TRUE : JNI_FALSE;
}


#ifdef __cplusplus
}
#endif
